import React, { useRef } from "react";
import { useState } from "react";
function Child(){
    let [num,setNum] = useState(0);
    let addNum = ()=>{
        setNum(num+1)
    }
    return (
        <div>
            <h1>我是一个子组件---{num}</h1>
        </div>
    )
}
function App(){
    // 创建Ref对象用来保存dom实例
    let h1Ref = useRef();
    // 使用useRef没有办法直接获取组件实例·
    let childRef = useRef();
    return (
        <div>
            <h1 ref={h1Ref}>useRef的基本用法</h1>
            <button onClick={()=>{
                console.log(h1Ref)
                h1Ref.current.style.color = 'red';
            }}>获取h1dom</button>
            <Child ref={childRef} />
            <button onClick={()=>{
                console.log(childRef.current)
            }}>获取child组件实例</button>
        </div>
    )
}

export default App;